package com.cjj.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.cjj.service.FollowDiaryService;
import com.cjj.vo.FollowDetailsVo;
import com.cjj.vo.FollowDiaryVo;
import com.github.pagehelper.PageInfo;
import com.guigu.mapper.TInterposeFollowupVisitMapper;
import com.guigu.pojo.TInterposeFollowupVisit;
import com.guigu.pojo.TMember;
import com.guigu.viwe.PageVo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/followDiary")
public class FollowDiaryController {

    @Resource
    private FollowDiaryService followDiaryService;
//分页查询
    @RequestMapping("/queryFollowDiary")
    public PageInfo<FollowDiaryVo> queryFollowDiary(@RequestBody PageVo<FollowDiaryVo> pageVo){
        return followDiaryService.queryFollowDiary(pageVo);
    }
// 文件上传
    @RequestMapping("/uploadcontroller")
    public String  uploadcontroller(@RequestBody() MultipartFile myfile){
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId =  "LTAI5tBDtxb5zU2extkovwUW";
        String accessKeySecret = "oFZi4U5KRMtq1kZy8igFLCXcOD9l2U";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "projectvue";
        //你上传到oss后的名字 会根据日期帮你创建文件夹。
        String objectName = String.valueOf(UUID.randomUUID());
        String hz=myfile.getOriginalFilename().substring(myfile.getOriginalFilename().indexOf(".")+1);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            InputStream inputStream = myfile.getInputStream();
            // 创建PutObject请求。
            ossClient.putObject(bucketName, objectName+"."+hz, inputStream);
        } catch (Exception oe) {

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        //https://qy151.oss-cn-hangzhou.aliyuncs.com/2022/6/10/20d3d7e6b5bb455cb548675501f7270fgdnj.jpg
        String url = "https://" + bucketName + "." + endpoint + "/" + objectName+"."+hz;
        return url;
    }
    //查找被干预人
    @RequestMapping("/queryInterferedPerson")
    public List<TMember> queryInterferedPerson(){
        return this.followDiaryService.queryInterferedPerson();
    }

    @RequestMapping("/addFollow")
    public int addFollow(@RequestBody FollowDetailsVo followDetailsVo){
        return  this.followDiaryService.addFollow(followDetailsVo);
    }

    @RequestMapping("/updateFollow")
    public int updateFollow(@RequestBody TInterposeFollowupVisit tInterposeFollowupVisit){
        return this.followDiaryService.updateFollow(tInterposeFollowupVisit);
    }
    @RequestMapping("/deleteRow")
    public int deleteRow(String filenumber){
        return this.followDiaryService.deleteRow(filenumber);
    }

}
